home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / getpwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  2.2 KB  |  108 lines  |  [TEXT/MPS ]

  1. /* getpwd.c - get the working directory */
  2.  
  3. #include "config.h"
  4.  
  5. #ifdef MPW
  6.  
  7. /* This should probably be more elaborate for MPW. */
  8.  
  9. char *
  10. getpwd ()
  11. {
  12.     return ":";
  13. }
  14.  
  15. #else /* n MPW */
  16.  
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20.  
  21. #ifndef errno
  22. extern int errno;
  23. #endif
  24.  
  25. /* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
  26.    BSD systems) now provides getcwd as called for by POSIX.  Allow for
  27.    the few exceptions to the general rule here.  */
  28.  
  29. #if !(defined (POSIX) || defined (USG) || defined (VMS))
  30. #include <sys/param.h>
  31. extern char *getwd ();
  32. #define getcwd(buf,len) getwd(buf)
  33. #define GUESSPATHLEN (MAXPATHLEN + 1)
  34. #else /* (defined (USG) || defined (VMS)) */
  35. extern char *getcwd ();
  36. /* We actually use this as a starting point, not a limit.  */
  37. #define GUESSPATHLEN 100
  38. #endif /* (defined (USG) || defined (VMS)) */
  39.  
  40. char *getenv ();
  41. char *xmalloc ();
  42.  
  43. #ifndef VMS
  44.  
  45. /* Get the working directory.  Use the PWD environment variable if it's
  46.    set correctly, since this is faster and gives more uniform answers
  47.    to the user.  Yield the working directory if successful; otherwise,
  48.    yield 0 and set errno.  */
  49.  
  50. char *
  51. getpwd ()
  52. {
  53.   static char *pwd;
  54.   static int failure_errno;
  55.  
  56.   char *p = pwd;
  57.   size_t s;
  58.   struct stat dotstat, pwdstat;
  59.  
  60.   if (!p && !(errno = failure_errno))
  61.     {
  62.       if (! ((p = getenv ("PWD")) != 0
  63.          && *p == '/'
  64.          && stat (p, &pwdstat) == 0
  65.          && stat (".", &dotstat) == 0
  66.          && dotstat.st_ino == pwdstat.st_ino
  67.          && dotstat.st_dev == pwdstat.st_dev))
  68.  
  69.     /* The shortcut didn't work.  Try the slow, ``sure'' way.  */
  70.     for (s = GUESSPATHLEN;  ! getcwd (p = xmalloc (s), s);  s *= 2)
  71.       {
  72.         int e = errno;
  73.         free (p);
  74. #ifdef ERANGE
  75.         if (e != ERANGE)
  76. #endif
  77.           {
  78.         errno = failure_errno = e;
  79.         p = 0;
  80.         break;
  81.           }
  82.       }
  83.  
  84.       /* Cache the result.  This assumes that the program does
  85.      not invoke chdir between calls to getpwd.  */
  86.       pwd = p;
  87.     }
  88.   return p;
  89. }
  90.  
  91. #else    /* VMS */
  92.  
  93. #ifndef MAXPATHLEN
  94. #define MAXPATHLEN 255
  95. #endif
  96.  
  97. char *
  98. getpwd ()
  99. {
  100.   static char *pwd = 0;
  101.  
  102.   if (!pwd) pwd = getcwd (xmalloc (MAXPATHLEN+1), MAXPATHLEN+1);
  103.   return pwd;
  104. }
  105.  
  106. #endif    /* VMS */
  107. #endif  /* MPW */
  108.